package main

import (
  "gitee.com/hacker61/go-binance/v2"
  "gitee.com/hacker61/go-binance/v2/futures"
  //"math/rand"
  //"time"
)

type ClientPool []*binance.Client
type FuturesClientPool []*futures.Client

var (
  lpServices []*binance.ListPricesService
  flpServices []*futures.ListPricesService
  iIndex int
)

func init() {
  //rand.Seed(time.Now().UnixNano())
  iIndex = 0
}

func NewClientPool(apiKey, secretKey string, srcIPs ...string) ClientPool {
  pool := make(ClientPool, 0)
  for _, srcIP := range srcIPs {
    newClient := binance.NewClient(apiKey, secretKey)
    newClient.SetSourceAddr(srcIP)
    pool = append(pool, newClient)
  }
  return pool
}

func NewFuturesClientPool(apiKey, secretKey string, srcIPs ...string) FuturesClientPool {
  pool := make(FuturesClientPool, 0)
  for _, srcIP := range srcIPs {
    newClient := futures.NewClient(apiKey, secretKey)
    newClient.SetSourceAddr(srcIP)
    pool = append(pool, newClient)
  }
  return pool
}

func (pool ClientPool) NewListPricesService() *binance.ListPricesService {
  if lpServices == nil {
    lpServices = make([]*binance.ListPricesService, 0)
    for _, client := range pool {
      lpService := client.NewListPricesService()
      lpServices = append(lpServices, lpService)
    }
  }
  //return lpServices[rand.Intn(len(lpServices))]
  iIndex++
  return lpServices[iIndex % len(lpServices)]
}

func (pool FuturesClientPool) NewListPricesService() *futures.ListPricesService {
  if flpServices == nil {
    flpServices = make([]*futures.ListPricesService, 0)
    for _, client := range pool {
      flpService := client.NewListPricesService()
      flpServices = append(flpServices, flpService)
    }
  }
  iIndex++
  return flpServices[iIndex % len(flpServices)]
}

func (pool ClientPool) One() *binance.Client {
  iIndex++
  return pool[iIndex % len(pool)]
}

func (pool FuturesClientPool) One() *futures.Client {
  iIndex++
  return pool[iIndex % len(pool)]
}